   
{mfdeclre.i}

/* STANDARD INCLUDE FOR MAINTENANCE COMPONENTS */
{pxmaint.i}

define input parameter nbr like wo_nbr.

define new shared variable comp like ps_comp.
define new shared variable qty as decimal.
define new shared variable eff_date as date.
define new shared variable wo_recno as recid.
define new shared variable leadtime like pt_mfg_lead.
define new shared variable prev_status like wo_status.
define new shared variable prev_release like wo_rel_date.
define new shared variable prev_due like wo_due_date.
define new shared variable prev_qty like wo_qty_ord.
define new shared variable del-yn like mfc_logical initial no.
define new shared variable deliv like wod_deliver.
define new shared variable wo_recno1     as recid.
define new shared variable joint_type    like wo_joint_type.
define new shared variable del-joint     like mfc_logical initial no.
define new shared variable joint_qtys    like mfc_logical initial no.
define new shared variable joint_dates   like mfc_logical initial no.
define new shared variable no_msg        like mfc_logical initial yes.
define new shared variable err_msg       as integer.
define new shared variable any_issued    like mfc_logical.
define new shared variable any_feedbk    like mfc_logical.
define new shared variable prev_site     like wo_site.
define new shared variable undo_all      like mfc_logical no-undo.
define new shared variable critical-part like wod_part    no-undo.
define new shared variable critical_flg  like mfc_logical no-undo.

define variable joint_wo   like wo_nbr.
define variable b_nbr      like wo_nbr.
define variable b_lot      like wo_lot.
define variable b_part     like wo_part.
define variable b_rel_date like wo_rel_date.
define variable b_due_date like wo_due_date.
define variable b_qty_ord  like wo_qty_ord.
define variable b_so_job   like wo_so_job.
define variable i as integer.
define variable nonwdays as integer.
define variable overlap as integer.
define variable workdays as integer.
define variable interval as integer.
define variable know_date as date.
define variable find_date as date.
define variable frwrd as integer.
define variable last_due as date.
define variable hours as decimal.
define variable queue like wc_queue.
define variable wait like wc_wait.
define variable last_op like wr_op.
define variable des like pt_desc1.
define variable wonbr like wo_nbr.
define variable wonbr1 like wo_nbr.
define variable wolot like wo_lot.
define variable wolot1 like wo_lot.
define variable part like wo_part.
define variable part1 like wo_part.
define variable bom  like wo_bom_code.
define variable bom1 like wo_bom_code.
define variable woreldate like wo_rel_date.
define variable woreldate1 like wo_rel_date.
define variable wostat_to like wo_status initial "E".

define variable wostat_from like wo_status label "Change from Status" initial "R".
define variable wostat1 as character format "x(12)" label "Original Status".
define variable wostat2 as character format "x(12)" label "Current Status".
define variable save_wonbr like wonbr.
define variable prev_wonbr like wonbr.
define variable prev_wolot like wo_lot.
define variable prev_recno as recid.
define variable phantom like mfc_logical initial no
   label "Include Phantom Items".
define variable site  like wo_site no-undo.
define variable site1 like wo_site no-undo.

define buffer wo_mstr1 for wo_mstr.

define variable l_ptstatus    like pt_status no-undo.
define variable l_tmp_message like msg_desc  no-undo.

/* DEFINE THE PERSISTENT HANDLE FOR THE PROGRAM wocmnrtn.p */
{pxphdef.i wocmnrtn}

eff_date = today.

assign
    wonbr = nbr
    wonbr1 = nbr
    wostat_from = "R"
    wostat_to = "E"
    .

repeat with frame b width 132 no-attr-space:
  /* FIND ROUTABLE, B STAT WOs FOR PREV WO. SKIP OTHER B STAT WOs*/
    if wostat_to <> "B" then
        find first wo_mstr where wo_nbr = prev_wonbr and wo_status = "B" and wo_site = prev_site and wo_lot > prev_wolot use-index wo_nbr no-lock no-error.

    repeat:
        if not available wo_mstr then leave.

        find ptp_det where ptp_part = wo_part and ptp_site = wo_site no-lock no-error.
        if available ptp_det then do:
            if ptp_pm_code = "R" and (ptp_phantom = no or phantom = yes) then leave.
        end.
        else do:
            find pt_mstr where pt_part = wo_part no-lock no-error.
            if available pt_mstr and pt_pm_code = "R" and (pt_phantom = no or phantom = yes) then leave.
        end.
     
        find next wo_mstr where wo_nbr = prev_wonbr and wo_status = "B" and wo_site = prev_site and wo_lot > prev_wolot use-index wo_nbr no-lock no-error.
    end.

    if not available wo_mstr then do:
        /* NO ROUTABLE WO, FIND PRIOR WO TO GET READY TO FIND NEXT */

        for first wo_mstr where recid(wo_mstr) = prev_recno no-lock:
        end.

        if not available wo_mstr then
            for first wo_mstr where wo_nbr >= wonbr and wo_nbr <= wonbr1 and wo_status = wostat_from use-index wo_nbr no-lock:
            end.
        else
            find next wo_mstr where wo_nbr >= wonbr and wo_nbr <= wonbr1 and wo_status = wostat_from use-index wo_nbr no-lock no-error.


        prev_recno = ?.

        if available wo_mstr then do:

            assign
                prev_recno = recid(wo_mstr)
                prev_wonbr = wo_nbr
                prev_wolot = wo_lot
                prev_site = wo_site.

            if phantom = no then do:
                find ptp_det where ptp_part = wo_part and ptp_site = wo_site no-lock no-error.
                if available ptp_det and ptp_phantom then next.
                if not available ptp_det then find pt_mstr where pt_part = wo_part no-lock no-error.
                if available pt_mstr and pt_phantom then next.
            end.
        end.
    end.

    if not available wo_mstr then leave.


  /* JOINT WOs ALREADY UPDATED AS A SET & MAY NOT CHG TO STAT B */
    if wo_joint_type <> "" and (wo_nbr = joint_wo or wostat_to = "B") then next.

    assign
        prev_wonbr = wo_nbr
        prev_wolot = wo_lot
        prev_site = wo_site
        wonbr = wo_nbr.
        
    if wo_status = wostat_to then next.

    assign
        wo_recno = recid(wo_mstr)
        comp = wo_part
        prev_status = wo_status
        prev_release = wo_rel_date
        prev_due = wo_due_date
        prev_qty = wo_qty_ord.

    if wo_qty_ord >= 0 then
        qty = max (wo_qty_ord - wo_qty_comp - wo_qty_rjct,0).
    else
        qty = min (wo_qty_ord - wo_qty_comp - wo_qty_rjct,0).

  /* IN ADDITION TO CHECKS ON TYPE/STATUS, CALL ACTIVITY   */
  /* RECORDING WORK ORDERS SHOULD NOT BE UPDATED HERE.     */
  /* RMA WORK ORDERS, HOWEVER, SHOULD BE ALLOWED.          */
    if (index("FRESCW",wo_type) = 0 or (index ("RE",wo_type) <> 0 and index ("ARC",wostat_to) <> 0) or (index ("F",wo_type) <> 0 and index ("EARC",wostat_to) <> 0)) and (wo_fsm_type = " " or wo_fsm_type = "RMA") then do:

        do transaction:
            find wo_mstr exclusive-lock where recid(wo_mstr) = wo_recno.
            wo_status = wostat_to.
        end.

        undo_all = no.

        {gprun.i ""wowomta.p""}

        if wo_joint_type <> "" and not any_issued and not any_feedbk then do:
        /* RE-IMPLODE JOINT PRODUCT WOs OR CHANGE THEIR STATUS */
            if (index("PBFC",prev_status) > 0 and index("FEAR",wo_status) > 0) then do:
           /* RE-ESTABLISH ITS EFFECTIVE JOINT WOs.*/
                {gprun.i ""wowomtf.p""}
                find wo_mstr no-lock where recid(wo_mstr) = wo_recno.
            end.
            else do:
           /* UPDATE ALL JP WOs IN THE SET (STATUS CHANGE ONLY) */
               {gprun.i ""wowomti.p""}
            end.
        end.

        if any_issued or any_feedbk or undo_all then
            do transaction:
                wo_status = prev_status.
            end.
    end.

  /* UPDATE STATUS CLOSE DATE AND USERID ON DISCRETE WORK ORDERS */
    {wostatcl.i}

  /* STRING TEXT FOR LETTER (e.g. EXPLODED for E) */
    {mfwostat.i wostat1 prev_status}
    {mfwostat.i wostat2 wo_status}

  /* DISPLAY JOINT PRODUCT ORDERS IN THE SET */
    if wo_joint_type <> "" then do:
        joint_wo = wo_nbr.
    end.
end.
